import { Button, Space } from 'ant-design-vue'
import moment from 'moment'
import { defineComponent, ref } from 'vue'

import { userApi } from '@/apis'
import qzyApi from '@/apis/qzyApi'
import { ProTable, ProTableProps, ProTableRef } from '@/components'
import { TABLE_FORMAT, TABLE_ITEM_RENDER, TABLE_RENDERER } from '@/constants'
import router from '@/router'
import { optionsToMap } from '@/utils'

export default defineComponent({
  name: 'QzyRedeemOrder',
  setup() {
    const statusOptions = [
      { label: '待推送erp', value: 0 },
      { label: '待发货', value: 1 },
      { label: '待收货', value: 2 },
      { label: '已完成', value: 3 }
    ]
    const statusMap = optionsToMap(statusOptions)
    const tableRef = ref({} as ProTableRef)
    const tableConfig = ref<ProTableProps>({
      fixedHeight: true,
      options: {
        border: true,
        align: 'center',
        proxyConfig: {
          form: true,
          props: {
            result: 'records',
            total: 'total'
          },
          ajax: {
            query: async ({ page, form }) => {
              const data = await qzyApi.getQzyorderlList({
                pageSize: page.pageSize,
                pageNo: page.currentPage,
                ...form
              })
              return data
            }
          }
        },
        formConfig: {
          items: [
            {
              title: '订单号',
              field: 'orderNo',
              itemRender: {
                name: TABLE_ITEM_RENDER.AInput,
                props: {
                  placeholder: '请输入活动名称',
                  style: {
                    width: '200px'
                  }
                }
              }
            },
            {
              title: '兑换手机号',
              field: 'mobile',
              itemRender: {
                name: TABLE_ITEM_RENDER.AInput,
                props: {
                  placeholder: '请输入活动名称',
                  style: {
                    width: '200px'
                  }
                }
              }
            },
            {
              title: '兑换时间',
              itemRender: {
                name: TABLE_RENDERER.FormItemDate,
                props: {
                  type: 'range',
                  placeholder: ['开始时间', '结束时间'],
                  valueFormat: 'x',
                  showTime: {
                    defaultValue: [
                      moment('00:00:00', 'HH:mm:ss'),
                      moment('23:59:59', 'HH:mm:ss')
                    ]
                  },
                  keys: ['startTime', 'endTime']
                }
              }
            },
            {
              title: '状态',
              field: 'orderStatus',
              itemRender: {
                name: TABLE_ITEM_RENDER.ASelect,
                props: {
                  placeholder: '全部',
                  allowClear: true
                },
                options: statusOptions
              }
            },
            {
              itemRender: {
                name: TABLE_RENDERER.FormItemBtns
              }
            }
          ]
        },
        columnConfig: {
          minWidth: 100,
          resizable: true
        },
        columns: [
          {
            title: '订单号',
            field: 'orderNo'
          },
          {
            title: '商品名称',
            field: 'prizeName'
          },
          {
            title: '订单状态',
            field: 'orderStatus',
            formatter: ({ cellValue }) => {
              return statusMap[cellValue]
            }
          },
          {
            title: '数量',
            field: 'num',
            formatter: ({ cellValue }) => {
              return 1
            }
          },
          {
            title: '兑换人',
            field: 'receiverName'
          },
          {
            title: '兑换手机号',
            field: 'receiverMobile'
          },
          {
            title: '兑换时间',
            field: 'orderTime',
            formatter: TABLE_FORMAT.Date
          },
          {
            title: '操作',
            fixed: 'right',
            slots: {
              default: 'operation'
            }
          }
        ],
        pagerConfig: {
          pageSize: 10
        }
      }
    })
    function handleDetail(row) {
      console.log(row.orderNo)
      router.push({ name: 'QzyOrderDetail', query: { orderNo: row.orderNo } })
    }
    return {
      tableRef,
      tableConfig,
      handleDetail
    }
  },
  render() {
    return (
      <div>
        <ProTable
          ref="tableRef"
          v-slots={{
            operation: ({ row }) => (
              <div>
                <Button
                  type="link"
                  size="small"
                  onClick={() => this.handleDetail(row)}
                >
                  订单详情
                </Button>
              </div>
            )
          }}
          {...this.tableConfig}
        />
      </div>
    )
  }
})
